package com.jingzhongbao.springboot.utils;

import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

/**
 * @Description: solr搜索工具类
 * @author 中宝
 * @date 2018/10/26 下午16:17
 */
public class SolrUtil {
	private static Logger logs = Logger.getLogger("SolrUtil");
	// 服务器连接
	private static SolrClient server;
	// 配置文件中获取索引服务器的地址
	private static String serverUrl = "";

	static {
		Properties properties = new Properties();
		try {
			InputStream fis = SolrUtil.class
					.getResourceAsStream("/application.properties");
			properties.load(fis);
			String solr_url = properties.getProperty("SOLR_URL");
			String solr_core = properties.getProperty("SOLR_CORE");
			serverUrl = solr_url + "/" + solr_core;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 初始化solr连接      
	 */
	public static SolrClient getSolrClient() {
		if (server == null) {
			logs.info("初始化solr连接，solr服务器连接地址为:" + serverUrl);
			server = new HttpSolrClient.Builder(serverUrl)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();
		}
		return server;
	}

}
